const {app, BrowserWindow, Notification, ipcMain} = require('electron')

let win
app.on('ready', () => {
  win = new BrowserWindow({
    width: 300,
    height: 300,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  })
  win.loadFile('./index.html')

  win.webContents.openDevTools(); //也可通过快捷键ctrl+shift+i

  setTimeout(handleIPC, 500)
})

function handleIPC() {
  /** case1: 演示 接受来自渲染进程的通信*/
  ipcMain.on('do-some-work', function (event, ...args) {
    // do some work
    console.log('接受到来自渲染进程发送的 do-some-work 事件, 其带的参数为:', args.toString())
    console.log(typeof args[0], args[0]); //number 1
    console.log(typeof args[1],args[1]);  //string string123
    console.log(typeof args[2],args[2]);  //object { a: 1, b: 2 }
    console.log(args[2]['a']); //1
    // 原型链无法传递 (隔代原型无法传递)
    console.log(typeof args[3],args[3]);
    // args[3].doA(); //会报错 doA is not a function
    console.log(typeof args[4], args[4]);
    console.log(args[4].getDate());


    //还可以回复消息给renderer
    event.reply('reply', 2, 'i have received ur message --- from main', {name:'ahhh',age:123});
  })

  ipcMain.handle('bbb',async(event, ...args)=>{
    //do something
    return 123; //会作为 await ipcRenderer.invoke('bbb', 'afwf') 的返回值接受
  })
}
